﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace ISRecord.Model
{
    public partial class Outbox
    {
        public override bool Equals(object obj)
        {
            Outbox theOther = obj as Outbox;
            if (theOther == null) return false;
            return this.OutboxNo.Equals(theOther.OutboxNo);
        }
        public override int GetHashCode()
        {
            int hash = 13;
            hash = (hash * 7) + OutboxNo.GetHashCode();
            //Note: if you have more fields in the compersion the add them as you do with OutboxNo field.
            return hash;
        }
        internal static string MaxOutboxNo()
        {
            using (Sender2006Entities context = new Sender2006Entities())
            {
                return 
                    (from outbox in context.Outbox
                        select outbox.OutboxNo).Max();
            }
        }

        #region "Maintain Changed Flage"
        public bool Changed
        { get; private set; }
        partial void OnOutboxNoChanged()
        {
            Changed = true;
        }
        partial void OnOutboxDateChanged()
        {
            Changed = true;
        }
        partial void OnSubjectChanged()
        {
            Changed = true;
        }
        partial void OnGoingToChanged()
        {
            Changed = true;
        }
        partial void OnAttachmentNoChanged()
        {
            Changed = true;
        }
        partial void OnNotesChanged()
        {
            Changed = true;
        }
        #endregion
       
          public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }
        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (string.IsNullOrEmpty(OutboxNo)) yield return new RuleViolation("رقم الصادر مطلوب");
            if (string.IsNullOrEmpty(OutboxDate)) yield return new RuleViolation("تاريخ الصادر مطلوب");
            if (string.IsNullOrEmpty(Subject)) yield return new RuleViolation("الموضوع مطلوب");
            if (string.IsNullOrEmpty(GoingTo)) yield return new RuleViolation("الجهة الصادر إليها مطلوبة");
            //
            if (!Helper.IsValidDate(OutboxDate)) yield return new RuleViolation("التاريخ المدخل غير صحيح");
            yield break;

        }
        public void Validate()
        {
            if (!IsValid) throw new InvalidOperationException("Rule violation privent saving!");
        }

    }
}
